package asia.lxq.cic.service;

import asia.lxq.cic.dao.ActivityMapper;
import asia.lxq.cic.dao.SignMapper;
import asia.lxq.cic.entity.Activity;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.entity.Sign;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SignService {

    @Autowired
    ActivityService activityService;
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    SignMapper signMapper;

    public Result<Boolean> signActivity(int activityId, int userId, String code){
        Activity activity=activityMapper.selectById(activityId);
        if (activityService.isAllow(activityId,userId).getData()) {
            QueryWrapper<Sign> signQueryWrapper=new QueryWrapper<>();
            signQueryWrapper.eq("sign_user_id",userId).eq("sign_activity_id",activityId);
            Sign sign = signMapper.selectOne(signQueryWrapper);
            if (sign==null)return Result.success(false,"你没有报名这个活动");
            if (activity.getActivityVerification().equals(code)) {
                long doneTime = activity.getActivityDone().getTime();
                long stageTime=activity.getActivityStage().getTime();
                long timestamp=new Date().getTime();
                if (timestamp<=doneTime&&timestamp>=stageTime){
                    sign.setSignIsSign("已签到");
                    signMapper.updateById(sign);
                    return Result.success(true,"签到成功");
                }else return Result.success(false,"当前不在签到时间范围内");
            }else return Result.success(false,"签到码不正确");
        }else return Result.success(false,"不符合活动的参与条件");
    }

    public Result<Boolean> reportActivity(int activityId, int userId, String reportContent){
        QueryWrapper<Sign> signQueryWrapper=new QueryWrapper<>();
        signQueryWrapper.eq("sign_user_id",userId).eq("sign_activity_id",activityId);
        Sign sign=signMapper.selectOne(signQueryWrapper);
        if (sign==null)return Result.success(false,"你没有报名这个活动");
        Activity activity=activityMapper.selectById(activityId);
        long doneTime = activity.getActivityDone().getTime();
        long timestamp=new Date().getTime();
        if (timestamp<doneTime)Result.success(true,"活动未结束，不能提前提交活动报告");
        sign.setSignReport(reportContent);
        signMapper.updateById(sign);
        return Result.success(true,"提交报告成功");
    }
}
